import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';

import '../entity/setting_entity.dart';
import '../main.dart';
import '../util/toast_util.dart';

class SettingCommon extends StatefulWidget {
  const SettingCommon({Key? key}) : super(key: key);

  @override
  State<SettingCommon> createState() => _SettingCommonState();
}

class _SettingCommonState extends State<SettingCommon>  {

  StreamSubscription? actionSubscription;
  SettingEntity? entity;
  final box = GetStorage();
  var _apiAddr = "192.168.2.100";
  // var _sip2ApiAddr = "http://192.168.2.100:5006";
  var _deviceNo = "device_id=test";
  var _keycode = 293;
  var _distance = 30;
  var _deviceName = '轩毅信息';
  final _analysisBook = 'xy'.obs;

  final List<DropdownMenuItem> _analysisBookRules =
  [
    const DropdownMenuItem(value: 'xy', child: Text('xy'),),
    const DropdownMenuItem(value: 'xm', child: Text('xm'),),
  ];

  @override
  initState() {
    super.initState();
    // box.remove("setting_entity");
    if(box.hasData('setting_entity')){
      entity = SettingEntity.fromJson(box.read("setting_entity"));
      if(entity!.apiAddr != null){
        _apiAddr = entity!.apiAddr!;
      }
      // if(entity!.sip2ApiAddr != null){
      //   _sip2ApiAddr = entity!.sip2ApiAddr!;
      // }
      if(entity!.keycode != null){
        _keycode = entity!.keycode!;
      }
      if(entity!.deviceNo != null){
        _deviceNo = entity!.deviceNo!;
      }
      if(entity!.distance != null){
        _distance = entity!.distance!;
      }
      if(entity!.deviceName != null){
        _deviceName = entity!.deviceName!;
      }
      if(entity!.analysisBook != null){
        _analysisBook.value = entity!.analysisBook!;
      }
    }else{
      entity = SettingEntity();
    }
  }


  _apiWidget(){
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Text("  api地址 ",   style: TextStyle(fontSize: 16.sp),),
        Container(
          width: 200.w,
          height: 30.h,
          margin: EdgeInsets.all(5.r),
          child: TextField(
            decoration:  InputDecoration(
              border:  const OutlineInputBorder(),
              contentPadding: const EdgeInsets.only(left: 5, right: 5, top: 0, bottom: 0).r,
            ),
            style: TextStyle(fontSize: 16.sp),
            onChanged: (value){
              _apiAddr = value;
            },
            controller: TextEditingController.fromValue(TextEditingValue(
                text: _apiAddr,
                selection: TextSelection.fromPosition(
                    TextPosition(
                        affinity: TextAffinity.downstream,
                        offset: _apiAddr.length))
            )),
          ),
        ),
      ],
    );
  }
  // _sip2ApiWidget(){
  //   return Row(
  //     mainAxisAlignment: MainAxisAlignment.start,
  //     children: [
  //       Text("  sip2Api地址 ",   style: TextStyle(fontSize: 16.sp),),
  //       Container(
  //         width: 200.w,
  //         height: 30.h,
  //         margin: EdgeInsets.all(5.r),
  //         child: TextField(
  //           decoration:  InputDecoration(
  //             border:  const OutlineInputBorder(),
  //             contentPadding: const EdgeInsets.only(left: 5, right: 5, top: 0, bottom: 0).r,
  //           ),
  //           style: TextStyle(fontSize: 16.sp),
  //           onChanged: (value){
  //             _sip2ApiAddr = value;
  //           },
  //           controller: TextEditingController.fromValue(TextEditingValue(
  //               text: _sip2ApiAddr,
  //               selection: TextSelection.fromPosition(
  //                   TextPosition(
  //                       affinity: TextAffinity.downstream,
  //                       offset: _sip2ApiAddr.length))
  //           )),
  //         ),
  //       ),
  //     ],
  //   );
  // }

  _deviceNameWidget(){
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Text("  设备名 ",   style: TextStyle(fontSize: 16.sp),),
        Container(
          width: 200.w,
          height: 30.h,
          margin: EdgeInsets.all(5.r),
          child: TextField(
            decoration:  InputDecoration(
              border:  const OutlineInputBorder(),
              contentPadding: const EdgeInsets.only(left: 5, right: 5, top: 0, bottom: 0).r,
            ),
            style: TextStyle(fontSize: 16.sp),
            onChanged: (value){
              _deviceName = value;
            },
            controller: TextEditingController.fromValue(TextEditingValue(
                text: _deviceName,
                selection: TextSelection.fromPosition(
                    TextPosition(
                        affinity: TextAffinity.downstream,
                        offset: _deviceName.length))
            )),
          ),
        ),
      ],
    );
  }

  _ruleWidget(){
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Text("  条码号解析规则 ",   style: TextStyle(fontSize: 16.sp),),
        Container(
          alignment: Alignment.center,
          margin: EdgeInsets.all(5.r),
          child: Obx(() => DropdownButton(
            value: _analysisBook.value,
            onChanged: (value){
              _analysisBook.value = value.toString();
            },
            items: _analysisBookRules,
          )),
        ),
      ],
    );
  }

  _keycodeWidget(){
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Text("  按钮指令代码 ",   style: TextStyle(fontSize: 16.sp),),
        Container(
          width: 200.w,
          height: 30.h,
          margin: EdgeInsets.all(5.r),
          child: TextField(
            keyboardType: TextInputType.number,
            decoration:  InputDecoration(
              border:  const OutlineInputBorder(),
              contentPadding: const EdgeInsets.only(left: 5, right: 5, top: 0, bottom: 0).r,
            ),
            style: TextStyle(fontSize: 16.sp),
            onChanged: (value){
              _keycode = int.parse(value);
            },
            controller: TextEditingController.fromValue(TextEditingValue(
                text: _keycode.toString(),
                selection: TextSelection.fromPosition(
                    TextPosition(
                        affinity: TextAffinity.downstream,
                        offset: _keycode.toString().length))
            )),
          ),
        ),
      ],
    );
  }

  _deviceNoWidget(){
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Text("  设备号",   style: TextStyle(fontSize: 16.sp),),
        Container(
          width: 200.w,
          height: 30.h,
          margin: EdgeInsets.all(5.r),
          child: TextField(
            decoration:  InputDecoration(
              border:  const OutlineInputBorder(),
              contentPadding: const EdgeInsets.only(left: 5, right: 5, top: 0, bottom: 0).r,
            ),
            style: TextStyle(fontSize: 16.sp),
            onChanged: (value){
              _deviceNo = value;
            },
            controller: TextEditingController.fromValue(TextEditingValue(
                text: _deviceNo,
                selection: TextSelection.fromPosition(
                    TextPosition(
                        affinity: TextAffinity.downstream,
                        offset: _deviceNo.length))
            )),
          ),
        ),
        TextButton(onPressed: () {
          _saveDevice();
        }, child: Text('保存配置',   style: TextStyle(fontSize: 16.sp))),
      ],
    );
  }
  _distanceWidget(){
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Text("  定位纠正",   style: TextStyle(fontSize: 16.sp),),
        Container(
          width: 200.w,
          height: 30.h,
          margin: EdgeInsets.all(5.r),
          child: TextField(
            decoration:  InputDecoration(
              border:  const OutlineInputBorder(),
              contentPadding: const EdgeInsets.only(left: 5, right: 5, top: 0, bottom: 0).r,
            ),
            keyboardType: TextInputType.number,
            style: TextStyle(fontSize: 16.sp),
            onChanged: (value){
              var v = int.tryParse(value);
              if(v != null){
                _distance = v;
              }
            },
            controller: TextEditingController.fromValue(TextEditingValue(
                text: _distance.toString(),
                selection: TextSelection.fromPosition(
                    TextPosition(
                        affinity: TextAffinity.downstream,
                        offset: _distance.toString().length))
            )),
          ),
        ),
      ],
    );
  }



  _saveDevice(){
    entity!.deviceNo = _deviceNo;
    entity!.apiAddr = _apiAddr;
    // entity!.sip2ApiAddr = _sip2ApiAddr;
    entity!.keycode = _keycode;
    entity!.distance = _distance;
    entity!.deviceName = _deviceName;
    entity!.analysisBook = _analysisBook.value;
    box.write('setting_entity', entity!.toJson());
    toastWarn('保存成功');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("配置"),
        leading: IconButton(
          icon: const Icon(Icons.arrow_back_ios),
          onPressed: (){

            if(box.hasData('setting_entity')){
              settingEntity = SettingEntity.fromJson(box.read("setting_entity"));
            }
            Get.back();
          },
        ),
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          _deviceNoWidget(),
          _deviceNameWidget(),
          _apiWidget(),
          // _sip2ApiWidget(),
          _keycodeWidget(),
          _ruleWidget(),
          _distanceWidget()
        ],
      ),
    );
  }
}

